using NUnit.Framework;
using Pathfinding;
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor.Build;
using UnityEngine;

public class NavigationService : FSMServiceiBase
{
    public override void OnAnimationEnd(PlayerState state)
    {
        base.OnAnimationEnd(state);
    }

    public override void OnBegin(PlayerState state)
    {
        base.OnBegin(state);
    }

    public override void OnDisable(PlayerState state)
    {
        base.OnDisable(state);
    }

    public override void OnEnd(PlayerState state)
    {
        base.OnEnd(state);
    }

    public override void OnUpdate(float normalizedTime, PlayerState state)
    {
        base.OnUpdate(normalizedTime, state);
        OnMove();
    }

    List<Vector3> _path = new List<Vector3>();
    public int state; // 0 空闲 1正在寻路 2返回寻路结果
    Vector3 _point; //寻路的目的地


    [HideInInspector]
    public int currentWaypoint; //当前路点索引

    [HideInInspector]
    public Vector3 _pathLast; // 路径最后一个位置

    [HideInInspector]
    public Action _success; //寻路结束后的回调 

    public void Move(Vector3 targetPos,Action successCb)
    {
        if(state == 0 || (state == 1 &&  _point != targetPos))
        {
            _point = targetPos;
            this._success = successCb;
            state = 1;
            var v = NavHelper.Instance.GetWalkNearestPosition(targetPos);
            player.seeker.StartPath(player._transform.position,v,OnPathComplete);
        }
    }

    private void OnPathComplete(Path p)
    {
        if(state ==1)
        {
            state = 2;
            
            if(p.error == false)
            {
                _path = p.vectorPath;
                if(_path.Count <= 1)
                {
                    currentWaypoint = 0;
                }
                else
                {
                    currentWaypoint = 1;
                }
                _pathLast = _path[_path.Count - 1];
                this.player._transform.LookTarget(_path[currentWaypoint]);
                this._success?.Invoke();
            }
            else
            {
                Stop();
            }
        }
    }

    private void OnMove()
    {
        if(_path == null) return;
        //每帧 移动到下一个路点 进行 移动 
        //判断 是否 接近 路点 是的话 更新到 下一个路点

        if(currentWaypoint >= _path.Count)
        {
            Stop();
            return;
        }
        else
        {
            var next_point = _path[currentWaypoint];
            Vector3 dir = next_point - player._transform.position;
            dir.y = 0;
            this.player.Move(dir * player.GetMoveSpeed(),false);

            if(Vector3.Distance(this.player._transform.position,next_point) <= 0.5f)
            {
                if(currentWaypoint >= _path.Count - 1) //到达最后一个点
                {
                    Stop();
                }
                else
                {
                    currentWaypoint += 1;
                    this.player._transform.LookTarget(_path[currentWaypoint]);
                }
            }
        }


    }

    public void Stop()
    {
        _path = null;
        state = 0;
    }

    public override void ReLoop(PlayerState state)
    {
        base.ReLoop(state);
    }

    public override void ReStart(PlayerState state)
    {
        base.ReStart(state);
    }

    internal bool IsEnd()
    {
        return _path == null;
    }
}
